home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / emerald / emrldsys.lha / Language / Runec / runec.c < prev   
Encoding:
C/C++ Source or Header  |  1990-08-17  |  7.0 KB  |  343 lines

  1. /* 
  2.  * @(#)runec.c    1.3  10/24/89
  3.  */
  4. #include <errno.h>
  5. #include <sys/types.h>
  6. #include <sys/file.h>
  7. #include <sys/uio.h>
  8. #include <sys/socket.h>
  9. #include <netinet/in.h>
  10. #include <stdio.h>
  11.  
  12. #ifndef EMDIR
  13. #define EMDIR "/scratch/eric/emerald/"
  14. #endif
  15.  
  16. extern int errno;
  17.  
  18. #include "Kernel/h/emPorts.h"
  19.  
  20. #define KERNELREQUESTSIZE 16
  21. #define IFLAGINDEX 10
  22.  
  23. #include <netdb.h>
  24. #include <signal.h>
  25.  
  26. int             talkSock;
  27. struct sockaddr_in     destSockDescr = {AF_INET};
  28. int             vflag = 0;
  29. int             iflag = 1;
  30. int             mflag = 0;
  31. char             mdata[100];
  32. #define NAMELIMIT    80
  33. char             name[NAMELIMIT];
  34. int             namelen;
  35. char            *filename = NULL;
  36. char *MLP1 = NULL, *MLP2 = NULL;
  37.  
  38. #ifdef STARTKERNEL
  39. char *startKernelArgs[] = {
  40.   "EmeraldKernel",
  41.   (char *) 0,
  42.   (char *) 0,
  43. };
  44. extern int callsys();
  45. #endif
  46.  
  47. int sendLoadDotoRequest()
  48. {
  49.   struct hostent    *myHost;
  50.   int             result = 1;
  51. #ifdef STARTKERNEL
  52.   char             binname[100];
  53.   int             try = 0
  54.  
  55. again:
  56.   if (++try > 2) {
  57.     result = 0;
  58.     goto cleanup;
  59.   } 
  60. #endif
  61.  
  62.   talkSock = socket(AF_INET, SOCK_STREAM, 0);
  63.   if (talkSock < 0) {
  64.     perror("runec: socket");
  65.     return(0);
  66.   }
  67.  
  68.   if (! mflag) {
  69.     if (gethostname(mdata, (int *)100) < 0) {
  70.       perror("runec: gethostname");
  71.       result = 0;
  72.       goto cleanup;
  73.     }
  74.   }
  75.   myHost = gethostbyname(mdata);
  76.   if (myHost == NULL) {
  77.     perror("runec: gethostbyname");
  78.     result = 0;
  79.     goto cleanup;
  80.   }
  81.  
  82.   bcopy(myHost->h_addr, (char *)&destSockDescr.sin_addr,
  83.     (unsigned)myHost->h_length);
  84.  
  85.  
  86.   {
  87.     char *emplanename;
  88.     int emPlane = 0;
  89.     int emPort = EMKERNELDEFAULTPORTNUMBER;
  90.     struct servent   *myService;
  91.     emplanename = (char *) getenv("EMPLANE");
  92.     if (emplanename != NULL) emPlane = atoi(emplanename);
  93. #ifdef DIKU
  94.     myService = getservbyname(EMKERNELSERVICEPORT, "tcp");
  95.     if(myService)
  96.       emPort = ntohs(myService->s_port);
  97. #endif
  98.     destSockDescr.sin_port = htons(emPort + emPlane*4 + 2);
  99.   }
  100.  
  101.   if (connect(talkSock, (struct sockaddr *)&destSockDescr, 
  102.       sizeof(destSockDescr)) == -1) {
  103. #ifdef STARTKERNEL
  104.     perror("sendToKernel: (connect)");
  105.     fprintf(stderr, "Trying to re-start kernel\n");
  106.     sprintf(binname, "%s/bin/startKernel", EMDIR);
  107.     if (mflag) startKernelArgs[1] = mdata;
  108.     callsys(binname, startKernelArgs, (char *)NULL,
  109.       (char *)NULL);
  110.     fprintf(stderr, "Started kernel, trying again.\n");
  111.     if (close(talkSock) < 0) perror("close");
  112.     goto again;
  113. #else
  114.     perror("runec: connect");
  115.     result = 0;
  116.     goto cleanup;
  117. #endif
  118.   }
  119.  
  120.   if (vflag) printf("Connected to %d\n", ntohs(destSockDescr.sin_port));
  121.   
  122.   if (vflag) printf("writing \"%.*s\"\n", namelen, name);
  123.   if (write(talkSock, name, namelen) != namelen) {
  124.     perror("runec: write");
  125.     result = 0;
  126.     goto cleanup;
  127.   }
  128.  
  129. cleanup:
  130.   if (!result) {
  131.     (void) shutdown(talkSock, 2);
  132.     (void) close(talkSock);
  133.   }
  134.   return(result);
  135. }
  136.  
  137. char *Usage = "Usage:\trunec <flags> <filename>\n\
  138. flags:\n\
  139. \t[-v] [-[mM] <machine name>] [-i] [-b]\n";
  140.  
  141. static void usagePanic()
  142. {
  143.   fprintf(stderr, Usage);
  144.   exit(1);
  145. }
  146.  
  147. parseFlags(argc, argv)
  148. int argc;
  149. char **argv;
  150. {
  151.   register int i;
  152.   FILE *inputFile;
  153.   char *newfilename;
  154.  
  155.   for (i = 1; i < argc; i++) {
  156.     if (argv[i][0] == '-') {
  157.       switch (argv[i][1]) {
  158.     case 'v':
  159.       vflag = 1;
  160.       break;
  161.     case 'i':
  162.       iflag = 1;
  163.       break;
  164.     case 'b':
  165.       iflag = 0;
  166.       break;
  167.     case 'm':
  168.     case 'M':
  169.       if (argv[i][2] == '\0') {
  170.         i++;
  171.         if (i < argc) {
  172.           strcpy(mdata, argv[i]);
  173.         } else {
  174.           usagePanic();
  175.         }
  176.       } else {
  177.         strcpy(mdata, &argv[i][2]);
  178.       }
  179.       mflag = 1;
  180.       break;
  181.     case 'X':
  182.       if (i < argc - 3) {
  183.         i++;
  184.         if (filename != NULL) usagePanic();
  185.         filename = argv[i];
  186.         i++;
  187.         MLP1 = argv[i];
  188.         i++;
  189.         MLP2 = argv[i];
  190.       } else {
  191.         usagePanic();
  192.       }
  193.       break;
  194.     default:
  195.       fprintf(stderr, "Unknown flag %s\n", argv[i]);
  196.       usagePanic();
  197.       break;
  198.       }
  199.     } else if (filename == NULL) {
  200.       filename = argv[i];
  201.     } else {
  202.       usagePanic();
  203.     }
  204.   }
  205.   if (filename == NULL) {
  206.     usagePanic();
  207.   } else {
  208.     if (!strcmp(filename+strlen(filename)-2, ".m")) {
  209.       filename[strlen(filename)-1] = 'g';
  210.       newfilename = filename;
  211.     } else if (!strcmp(filename+strlen(filename)-2, ".g")) {
  212.       newfilename = filename;
  213.     } else {
  214.       newfilename = (char *) malloc(strlen(filename)+5);
  215.       sprintf(newfilename, "%s.g", filename);
  216.     }
  217.     inputFile = fopen(newfilename, "r");
  218.     if (inputFile == NULL) {
  219.       fprintf(stderr, "Can't find \"%s\".\n", newfilename);
  220.       usagePanic();
  221.     }
  222.     fgets(name, 80, inputFile);
  223.     if (*name == '#') fgets(name, 80, inputFile);
  224.     if (*name && name[strlen(name)-1] == '\n') name[strlen(name)-1] = '\0';
  225.     if (strlen(name) != KERNELREQUESTSIZE) {
  226.       fprintf(stderr,
  227.     "File %s is not an Emerald compiler output file (wrong size).\n", newfilename);
  228.       usagePanic();
  229.     }
  230.     fclose(inputFile);
  231.     if (strncmp(name, "0x", 2)) {
  232.       fprintf(stderr, "File %s is not an Emerald compiler output file (bad stuff).\n",
  233.     newfilename);
  234.       usagePanic();
  235.     }
  236.     name[IFLAGINDEX] = (iflag ? 'T' : 'F');
  237.     namelen = KERNELREQUESTSIZE;
  238.     /*
  239.      * Stuff to add MLP arguments goes here
  240.      */
  241.     if (MLP1 && MLP2) {
  242.       sprintf(&name[namelen], "%4d", strlen(MLP1));
  243.       namelen += 4;
  244.       bcopy(MLP1, &name[namelen], strlen(MLP1));
  245.       namelen += strlen(MLP1);
  246.       bcopy(MLP2, &name[namelen], strlen(MLP2));
  247.       namelen += strlen(MLP2);
  248.       if (namelen > NAMELIMIT) {
  249.     fprintf(stderr, "Generated name is too long\n");
  250.     usagePanic();
  251.       }
  252.     }
  253.   }
  254. }  
  255.   
  256. char buffer[BUFSIZ];
  257.  
  258. doit(in, out)
  259. int in, out;
  260. {
  261.   int nBytes;
  262.   while ((nBytes = read(in, buffer, BUFSIZ)) > 0) {
  263.     if (write(out, buffer, nBytes) < 0) break;
  264.   }
  265. }
  266.  
  267. main(argc, argv)
  268. int argc;
  269. char **argv;
  270. {
  271.   int child;
  272.   parseFlags(argc, argv);
  273.   if (sendLoadDotoRequest()) {
  274.     if (iflag) {
  275.       child = fork();
  276.       if (child < 0) {
  277.     perror("Runec: fork");
  278.     exit(1);
  279.       }
  280.       if (child > 0) {
  281.     doit(talkSock, 1);
  282.     if (shutdown(talkSock, 0) < 0) {
  283.       perror("runec (shutdown)");
  284.       exit(1);
  285.     }
  286.     kill(child, SIGKILL);
  287.     while (wait(0) != child) ;
  288.       } else{
  289.     doit(0, talkSock);
  290.     if (shutdown(talkSock, 1) < 0) {
  291.       perror("runec (shutdown)");
  292.       exit(1);
  293.     }
  294.       }
  295.     }
  296.   }
  297. }
  298.  
  299. #ifdef STARTKERNEL
  300. /*
  301.  * Call the program named f with the arguments v, and stdin and out
  302.  * directed to inf and outf respectively.
  303.  */
  304. #include <sys/wait.h>
  305.  
  306. int callsys(f, v, inf, outf)
  307. char *f, **v, *inf, *outf;
  308. {
  309.   int t;
  310.   union wait status;
  311.   register char **cpp;
  312.   
  313.   t = vfork();
  314.   if (t == -1) {
  315.     fprintf(stderr, "No more processes\n");
  316.     return (100);
  317.   }
  318.   if (t == 0) {        /* I am the child */
  319.     int infd, outfd;
  320.     if (inf != NULL) {
  321.       infd = open(inf, O_RDONLY, 0666);
  322.       (void) dup2(infd, 0);
  323.     }
  324.     if (outf != NULL) {
  325.       outfd = open(outf, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  326.       (void) dup2(outfd, 1);
  327.     }
  328.     execv(f, v);
  329.     fprintf(stderr, "Can't find %s\n", f);
  330.     (void) fflush(stderr);
  331.     _exit(100);
  332.   }
  333.   while (t != wait(&status))
  334.     ;
  335.   if ((t=status.w_termsig) != 0 && t!=14) {
  336.     if (t!=2) {
  337.       fprintf(stderr, "Fatal error in %s\n", f);
  338.     }
  339.   }
  340.   return (status.w_retcode);
  341. }
  342. #endif
  343.